package com.winexpress.support;

import com.winexpress.util.SessionAttributes;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.ws.WebServiceRef;

@WebServlet(name = "EnquiryManagement", urlPatterns = {"/enquiryManagement/*"})
public class EnquiryManagement extends HttpServlet {

    @WebServiceRef(wsdlLocation = "WEB-INF/wsdl/localhost_8080/WineXpress-war/EnquiryManagementWS.wsdl")
    private EnquiryManagementWS_Service service;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, Exception_Exception {
        response.setContentType("text/html;charset=UTF-8");

        RequestDispatcher dispatcher;
        ServletContext servletContext = getServletContext();
        HttpSession session = request.getSession();

        String page = request.getPathInfo();
        page = page.substring(1);

        //Get Number of Items in Cart
        int count = 0;

        if (session.getAttribute("items") == null) {
            int i = 0;
            request.setAttribute("itemsnum", i);
        } else {
            String items = (String) session.getAttribute("items");
            String[] array = items.split("\\;");
            for (int i = 1; i < array.length; i = i + 2) {
                count = count + Integer.parseInt(array[i]);
            }
            request.setAttribute("itemsnum", count);
        }
        //End Number of Items in Cart

        if ("submitEnquiryResult".equals(page)) {
            request.setAttribute("result", submitEnquiry(request));
        } else if ("adminPendingEnquiry".equals(page)) {
             if (session.getAttribute(SessionAttributes.USER_ID) == null || session.getAttribute(SessionAttributes.IS_ADMIN) == null ||session.getAttribute(SessionAttributes.IS_ADMIN).toString().equals("false")) {
                response.sendRedirect("/WineXpressClient/loginFail.jsp");
                 System.out.println(session.getAttribute(SessionAttributes.IS_ADMIN));
                return;
            }         
           
            
            com.winexpress.support.EnquiryManagementWS port = service.getEnquiryManagementWSPort();
            List<Enquiry> list = port.listPendingEnquiry();
            request.setAttribute("EnquiryList", list);
        } else if ("adminRespondedEnquiry".equals(page)) {
            if (session.getAttribute(SessionAttributes.USER_ID) == null || session.getAttribute(SessionAttributes.IS_ADMIN) == null ||session.getAttribute(SessionAttributes.IS_ADMIN).toString().equals("false")) {
                response.sendRedirect("/WineXpressClient/loginFail.jsp");
                 System.out.println(session.getAttribute(SessionAttributes.IS_ADMIN));
                return;
            }        
            com.winexpress.support.EnquiryManagementWS port = service.getEnquiryManagementWSPort();
            List<Enquiry> list = port.listRespondedEnquiry();
            request.setAttribute("respondedEnquiryList", list);
        } else if ("customerViewEnquiry".equals(page)) {
            if (session.getAttribute(SessionAttributes.USER_ID) == null) {
                response.sendRedirect("/WineXpressClient/loginFail.jsp");
            }
            
            //System.out.println(session.getAttribute(SessionAttributes.USER_ID));
            com.winexpress.support.EnquiryManagementWS port = service.getEnquiryManagementWSPort();
            String user = null;

            user = String.valueOf(session.getAttribute(SessionAttributes.USER_ID));
            EnquiryToObject enq = new EnquiryToObject();
            enq.setUid(Long.parseLong(user));

            List<Enquiry> list = port.customerViewEnquiry(enq);
            request.setAttribute("customerEnquiryList", list);
        } else if ("customerViewEnquiryPending".equals(page)) {
            if (session.getAttribute(SessionAttributes.USER_ID) == null) {
                response.sendRedirect("/WineXpressClient/loginFail.jsp");
            }
           

            com.winexpress.support.EnquiryManagementWS port = service.getEnquiryManagementWSPort();

            String user = String.valueOf(session.getAttribute(SessionAttributes.USER_ID));
            EnquiryToObject enq = new EnquiryToObject();
            enq.setUid(Long.parseLong(user));
            List<Enquiry> list = port.customerViewEnquiryPending(enq);
            request.setAttribute("customerEnquiryList", list);
        } else if ("customerViewDetailedEnquiry".equals(page)) {
            com.winexpress.support.EnquiryManagementWS port = service.getEnquiryManagementWSPort();
            String viewid = request.getParameter("viewID");
            EnquiryToObject enq = new EnquiryToObject();
            enq.setEnquiryId(Long.parseLong(viewid));
            Enquiry enquiry = port.retrieveEnquiryByEid(enq);
            System.out.println(enquiry.getId());
            String eid = String.valueOf(enquiry.getId());
            String cid = String.valueOf(enquiry.getCustomer().getId());
            String aid = String.valueOf(enquiry.getAdmin().getId());
            String category = enquiry.getCategory();
            String subject = enquiry.getSubject();
            String content = enquiry.getContent();
            String dc = String.valueOf(enquiry.getDateCreated()).substring(0, 10);
            String dr = String.valueOf(enquiry.getDateResponded()).substring(0, 10);
            String reply = enquiry.getReply();

            request.setAttribute("eid", eid);
            request.setAttribute("cid", cid);
            request.setAttribute("dc", dc);
            request.setAttribute("category", category);
            request.setAttribute("subject", subject);
            request.setAttribute("content", content);

            request.setAttribute("reply", reply);
            request.setAttribute("aid", aid);
            request.setAttribute("dr", dr);

        } else if ("customerViewDetailedEnquiryPending".equals(page)) {
            com.winexpress.support.EnquiryManagementWS port = service.getEnquiryManagementWSPort();
            String viewid = request.getParameter("viewID");
            EnquiryToObject enq = new EnquiryToObject();
            enq.setEnquiryId(Long.parseLong(viewid));
            Enquiry enquiry = port.retrieveEnquiryByEid(enq);

            String eid = String.valueOf(enquiry.getId());
            String category = enquiry.getCategory();
            String subject = enquiry.getSubject();
            String content = enquiry.getContent();
            String dc = String.valueOf(enquiry.getDateCreated()).substring(0, 10);

            request.setAttribute("eid", eid);

            request.setAttribute("dc", dc);
            request.setAttribute("category", category);
            request.setAttribute("subject", subject);
            request.setAttribute("content", content);

        } else if ("adminAllEnquiry".equals(page)) {
            if (session.getAttribute(SessionAttributes.USER_ID) == null || session.getAttribute(SessionAttributes.IS_ADMIN) == null ||session.getAttribute(SessionAttributes.IS_ADMIN).toString().equals("false")) {
                response.sendRedirect("/WineXpressClient/loginFail.jsp");
                 System.out.println(session.getAttribute(SessionAttributes.IS_ADMIN));
                return;
            }        
            com.winexpress.support.EnquiryManagementWS port = service.getEnquiryManagementWSPort();
            List<Enquiry> list = port.listAllEnquiry();
            request.setAttribute("allEnquiryList", list);
        } else if ("viewPendingEnquiry".equals(page)) {
            com.winexpress.support.EnquiryManagementWS port = service.getEnquiryManagementWSPort();

            String viewid = request.getParameter("viewID");
            EnquiryToObject enq = new EnquiryToObject();
            enq.setEnquiryId(Long.parseLong(viewid));
            Enquiry enquiry = port.retrieveEnquiryByEid(enq);
            String eid = String.valueOf(enquiry.getId());
            String cid = String.valueOf(enquiry.getCustomer().getId());
            String category = enquiry.getCategory();
            String subject = enquiry.getSubject();
            String content = enquiry.getContent();
            String dc = String.valueOf(enquiry.getDateCreated()).substring(0, 10);
            request.setAttribute("eid", eid);
            request.setAttribute("cid", cid);
            request.setAttribute("category", category);
            request.setAttribute("subject", subject);
            request.setAttribute("content", content);
            request.setAttribute("dc", dc);

        } else if ("viewRespondedEnquiry".equals(page)) {
            com.winexpress.support.EnquiryManagementWS port = service.getEnquiryManagementWSPort();
            //System.out.println(port.retrieveEnquiryByEid(request.getParameter("viewID")));
            String viewid = request.getParameter("viewID");
            EnquiryToObject enq = new EnquiryToObject();
            enq.setEnquiryId(Long.parseLong(viewid));
            Enquiry enquiry = port.retrieveEnquiryByEid(enq);
            String eid = String.valueOf(enquiry.getId());
            String cid = String.valueOf(enquiry.getCustomer().getId());
            String aid = String.valueOf(enquiry.getAdmin().getId());
            String category = enquiry.getCategory();
            String subject = enquiry.getSubject();
            String content = enquiry.getContent();
            String dc = String.valueOf(enquiry.getDateCreated()).substring(0, 10);
            String dr = String.valueOf(enquiry.getDateResponded()).substring(0, 10);

            String reply = enquiry.getReply();
            request.setAttribute("eid", eid);
            request.setAttribute("cid", cid);
            request.setAttribute("aid", aid);
            request.setAttribute("category", category);
            request.setAttribute("subject", subject);
            request.setAttribute("content", content);
            request.setAttribute("dc", dc);
            request.setAttribute("reply", reply);
            request.setAttribute("dr", dr);

        } else if ("replyPendingEnquiry".equals(page)) {
            if (session.getAttribute(SessionAttributes.USER_ID) == null || session.getAttribute(SessionAttributes.IS_ADMIN) == null ||session.getAttribute(SessionAttributes.IS_ADMIN).toString().equals("false")) {
                response.sendRedirect("/WineXpressClient/loginFail.jsp");
                 System.out.println(session.getAttribute(SessionAttributes.IS_ADMIN));
                return;
            }        
            com.winexpress.support.EnquiryManagementWS port = service.getEnquiryManagementWSPort();
            EnquiryToObject enq = new EnquiryToObject();
            String user = null;
            user = String.valueOf(session.getAttribute(SessionAttributes.USER_ID));
            enq.setAdminid(Long.parseLong(user));
            String eid = String.valueOf(request.getParameter("enquiryid"));
            enq.setEnquiryId(Long.parseLong(eid));
            String reply = request.getParameter("reply");
            enq.setReply(reply);

            Enquiry enquiry = port.replyEnquiry(enq);
            request.setAttribute("reply", reply);
            request.setAttribute("aid", user);

            String cid = String.valueOf(enquiry.getCustomer().getId());
            String category = enquiry.getCategory();
            String subject = enquiry.getSubject();
            String content = enquiry.getContent();
            String dc = String.valueOf(enquiry.getDateCreated()).substring(0, 10);
            request.setAttribute("eid", eid);
            request.setAttribute("cid", cid);
            request.setAttribute("category", category);
            request.setAttribute("subject", subject);
            request.setAttribute("content", content);
            request.setAttribute("dc", dc);
            try {
                port.replyEnquiryEmail(enq);
            } catch (Exception_Exception ex) {
                Logger.getLogger(EnquiryManagement.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        dispatcher = servletContext.getNamedDispatcher(page);
        if (dispatcher == null) {
            dispatcher = servletContext.getNamedDispatcher("Error");
        }
        dispatcher.forward(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (Exception_Exception ex) {
            Logger.getLogger(EnquiryManagement.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (Exception_Exception ex) {
            Logger.getLogger(EnquiryManagement.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }

    private String submitEnquiry(HttpServletRequest request) throws Exception_Exception {
        com.winexpress.support.EnquiryManagementWS port = service.getEnquiryManagementWSPort();
        com.winexpress.support.EnquiryManagementWS port1 = service.getEnquiryManagementWSPort();
        String subject = request.getParameter("subject");
        String category = request.getParameter("category");
        String content = request.getParameter("content");

        HttpSession session = request.getSession();
        String user = null;
        user = String.valueOf(session.getAttribute(SessionAttributes.USER_ID));
        Long uid = Long.parseLong(user);
        String email = String.valueOf(session.getAttribute(SessionAttributes.USER_EMAIL));
        //System.out.println(email);
        EnquiryToObject enq = new EnquiryToObject();
        enq.setUid(uid);
        enq.setSubject(subject);
        enq.setCategory(category);
        enq.setContent(content);
        enq.setEmail(email);
        String enquiryID = port.submitEnquiry(enq);
        enq.setEnquiryId(Long.parseLong(enquiryID));
        port1.sendGroupEmail(enq);
        return enquiryID;
    }

}
